package com.yugu.park.system.service.impl;

import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import com.github.pagehelper.PageInfo;
import com.yugu.util.SearchPage;
import com.yugu.park.system.mapper.SysDeptMapper;
import com.yugu.park.dto.TreeNode;
import com.yugu.park.system.entity.SysDeptEntity;
import com.yugu.park.system.service.SysDeptService;
import com.github.pagehelper.PageHelper;

@Service
@Transactional
public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDeptEntity> implements SysDeptService {
      @Resource
	  private SysDeptMapper mapper;
	@Override
	public PageInfo<SysDeptEntity> page(SearchPage<SysDeptEntity> search) {
		PageHelper.startPage(search.getPageNum(),search.getPageSize());
        List<SysDeptEntity> list = mapper.page(search.getSearch());
        return PageInfo.of(list);
	}
	@Override
	public List<TreeNode> selectDeptTreeList(){
//		Wrapper<SysDeptEntity> wrapper = Wrappers
//                .<SysDeptEntity>lambdaQuery()
//                .eq(SysDeptEntity::getStatus, "0");
//		List<SysDeptEntity> list = this.list(wrapper);
		List<TreeNode> list = mapper.selectTree();
		List<TreeNode> result = new ArrayList<TreeNode>();
		for(TreeNode per:list) {
        	String pId = per.getPId();
        	if ("0".equals(pId)) {
        		result.add(per);
            }
        	for(TreeNode it:list) {
        		if (it.getPId().equals(per.getId())) {
                    if (per.getChildren() == null) {
                    	per.setChildren(new ArrayList<TreeNode>());
                    }
                    per.getChildren().add(it);
                }
        	}
        }
		return result;
	}

}
